<?php
namespace jy_baojfee;
defined('BY_JYA') or exit('error');
/*
	分红佣金
	
*/
class commission extends \plugin{
	/*
		生成佣金
	*/
	function create($order){
		$isopen=intval($this->setting['isopen']);
		if(!$isopen){
			return;
		}
		$this->order=$order;
		$member=$this->get('member',array('mid'=>$order['mid']),array('mid','is_agent','agent_id','level_id'));
        if(!$member['mid']){
			$member=$this->model('member')->checkmember($order['mid']);
		}else{
			if(!$member['is_agent'] && ($this->setting['become']==2 || $this->setting['become']==3 || $this->setting['become']==1)){
				$this->model('member')->checkmember($order['mid']);
			}
		}
		//复购倍率
		if($this->order['goods']){
			foreach($this->order['goods'] as $goods){
				$total_price=$goods['total_price']?$goods['total_price']:pdo_getcolumn('jy_weishop_order_goods',array('id'=>$goods['id']),'total_price');
				$goods_commission=$this->get('goods',array('goods_id'=>$goods['goodsid']));
				$candai=$goods_commission['candai'];
                if($goods_commission['buybei']>0){
                    $redfee=$goods_commission['buybei']*$total_price;
                    $this->update('member',array('redfee +='=>$redfee),array('mid'=>$this->order['mid'], 'uniacid' => $_SESSION['uniacid']));
                }
			}
		}
		if($member['level_id']){
			$this->lastlevel=$this->getcolumn('level',array('id'=>$member['level_id']),'level');
		}else{
			$this->lastlevel=0;
		}
		//自购开始
		$agent_id=$member['mid'];
		//自购升级
		if($member['is_agent']){
			$this->model('member')->update_agent_level($order['mid'],$order['id']);
		}
		//现在支付方式分佣
		if($this->setting['pay_type']){
			$paylog=pdo_get('core_paylog',array('id'=>$order['pay_id']),array('pay_type','di_credit','di_fee','fee'));
			if($paylog['di_fee']>0){
				if(strpos($this->setting['pay_type'],';'.$paylog['di_credit'].';') !== false){
					$this->order['price'] -= $paylog['di_fee'];
				}
				if(strpos($this->setting['pay_type'],';'.$order['paytype'].';') !== false){
					$this->order['price'] -= $paylog['fee']-$paylog['di_fee'];
				}
				//两种支付都不支持
				if(strpos($this->setting['pay_type'],';'.$paylog['di_credit'].';') !== false && strpos($this->setting['pay_type'],';'.$order['paytype'].';') !== false){
					return;
				}
			}elseif(strpos($this->setting['pay_type'],';'.$order['paytype'].';') !== false){
				return;
			}
		}
		if($this->order['price']<=0){
			return;
		}
		$this->commission_order=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$this->order['mid'],
				'ordersn'=>$this->order['ordersn'],
				'order_price'=>$this->order['price'],
				'plugin_name'=>$this->order['plugin_name']?:'',
				'commission_money'=>0,
				'ji_money'=>0,
				'jifen'=>0,
				'createtime'=>TIMESTAMP,
		);		
		//佣金计算方式，是否扣除运费
		if($this->setting['commissionfeetype']==1){
			$this->commission_order['commission_price']=$this->order['price'];
		}elseif($this->setting['commissionfeetype']==3){
			$this->commission_order['commission_price']=$this->order['price'] + $this->order['deductcredit'] + $this->order['deductcredit2'];
		}else{
			$this->commission_order['commission_price']=$this->order['price']-$this->order['dispatchprice'];
		}
        $levelnum=18;
		//当分红层级大于0时，逐次寻找上级推荐人并计算佣金
		for($i=0;$i<$levelnum;$i++){
			if(!$agent_id){
				break;
			}
			$member=$this->get('member',array('mid'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
			if(!$i){
				$this->agent_id1=$member['agent_id'];
			}
			$this->create_commission($member,$i);
            $agent_id=$member['agent_id'];
			$this->lastlevel=$member['level_id'];
		}
		//生成分红订单
		$indata=$this->commission_order;
		unset($indata['ji_money']);
		unset($indata['maxlevel']);
		unset($indata['isarea']);
		$this->insert('order',$indata);
		if($order['sendtype']==3){
			$status=pdo_getcolumn('jy_weishop_order',array('id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']),'status');
			if($status==3){
				$this->order_complete($order);
			}
		}
		return true;
	}
	/*
		团队收益
	*/
	function create_commission($member,$commission_level=0){
		if($member && $member['is_agent'] && $member['stop']==0){
			//相应会员存在且是加盟商且状态正常			
			if(!$member['level_id']){
				//获取默认等级佣金比例
                $level=$this->plugin_setting('default_level');
				$level_id=0;

			}else{
				$level=$this->get('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$level_id=$level['id'];
			}
            $gxfee_rate1=$level['gxfee_rate1'];
            $gxfee_rate2=$level['gxfee_rate2'];
			$gxfee_rate3=$level['gxfee_rate3'];
            $tui_rate=$level['tui_rate'];
            $jd_rate=$level['jd_rate'];
            $ld_rate=$level['ld_rate'];
            $gxfee1=0;$gxfee2=0;$gxfee3=0;$tuifee=0;$jdfee=0;$redfee=0;$ldfee=0;
			$order_price=$this->commission_order['commission_price'];
			$remark=array(
				'level_id'=>$level_id,
				'commission_level'=>$commission_level,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);
			
			//先检查是否存在商品独立佣金规则，如果存在则对订单相应商品部分金额单独计算佣金
			$total_lastcostprice=0;
			//非按笔计算
			$this->needorder=false;
			if($this->order['goods']){
				foreach($this->order['goods'] as $goods){
					if(!$goods['fee_desc'] && $goods['merchid']){
						model('jy_weishop/order')->setfee_desc($goods['orderid']);
					}
					$total_price=$goods['total_price']?$goods['total_price']:pdo_getcolumn('jy_weishop_order_goods',array('id'=>$goods['id']),'total_price');
					$costprice=pdo_getcolumn('jy_weishop_goods',array('id'=>$goods['goodsid']),'costprice');
					$total_costprice=$goods['total']*$costprice;
					$total_lastcostprice +=$total_costprice;
					$goods_commission=$this->get('goods',array('goods_id'=>$goods['goodsid']));
					$candai=$goods_commission['candai'];
					if(!$goods_commission['id']){
						$this->needorder=true;
						continue;
					}
					if($goods_commission['ignore']){
						$order_price=$order_price-$goods['total_price'];
						continue;
					}
					//如果商品单独设置佣金，将减少本商品的金额
					$order_price=$order_price-$total_price;
					$goods_commission['commissions']=$goods_commission['commissions']?json_decode($goods_commission['commissions'],true):array();
					foreach($goods_commission['commissions'] as $gc){
						if(!$gc['level_id']){
                            $demogoods_gxfee_rate1=$gc['gxfee_rate1'];
                            $demogoods_gxfee_rate2=$gc['gxfee_rate2'];
							$demogoods_gxfee_rate3=$gc['gxfee_rate3'];
                            $demogoods_tui_rate=$gc['tui_rate'];
                            $demogoods_jd_rate=$gc['jd_rate'];
						}
						if($gc['level_id']!=$level_id){
							continue;
						}else{
                            $goods_gxfee_rate1=$gc['gxfee_rate1'];
                            $goods_gxfee_rate2=$gc['gxfee_rate2'];
							$goods_gxfee_rate3=$gc['gxfee_rate3'];
                            $goods_tui_rate=$gc['tui_rate'];
                            $goods_jd_rate=$gc['jd_rate'];
							break;
						}
					}
					$goods_gxfee_rate1=$goods_gxfee_rate1?:$demogoods_gxfee_rate1;
                    $goods_gxfee_rate2=$goods_gxfee_rate2?:$demogoods_gxfee_rate2;
					$goods_gxfee_rate3=$goods_gxfee_rate3?:$demogoods_gxfee_rate3;
                    $goods_tui_rate=$goods_tui_rate?:$demogoods_tui_rate;
                    $goods_jd_rate=$goods_jd_rate?:$demogoods_jd_rate;
                    if(substr($goods_gxfee_rate1,-1)=='%'){
                        $goods_gxfee1=($total_price*substr($goods_gxfee_rate1,0,-1))/100;
                    }else{
                        $goods_gxfee1=$goods_gxfee_rate1*$goods['total'];
                    }
					$goods_gxfee1=intval($goods_gxfee1*100)/100;
					$gxfee1=intval($gxfee1*100)/100;
                    $gxfee1 +=$goods_gxfee1;
                    if(substr($goods_gxfee_rate2,-1)=='%'){
                        $goods_gxfee2=($total_price*substr($goods_gxfee_rate2,0,-1))/100;
                    }else{
                        $goods_gxfee2=$goods_gxfee_rate2*$goods['total'];
                    }
					$goods_gxfee2=intval($goods_gxfee2*100)/100;
                    $gxfee2 +=$goods_gxfee2;
					if(substr($goods_gxfee_rate3,-1)=='%'){
                        $goods_gxfee3=($total_price*substr($goods_gxfee_rate3,0,-1))/100;
                    }else{
                        $goods_gxfee3=$goods_gxfee_rate3*$goods['total'];
                    }
					$goods_gxfee3=intval($goods_gxfee3*100)/100;
                    $gxfee3 +=$goods_gxfee3;
					if(substr($goods_tui_rate,-1)=='%'){
						$goods_tuifee=($total_price*substr($goods_tui_rate,0,-1))/100;
					}else{
						$goods_tuifee=$goods_tui_rate*$goods['total'];
					}
					$goods_tuifee=intval($goods_tuifee*100)/100;
					$tuifee +=$goods_tuifee;
					if(substr($goods_jd_rate,-1)=='%'){
						$goods_jdfee=($total_price*substr($goods_jd_rate,0,-1))/100;
					}else{
						$goods_jdfee=$goods_jd_rate*$goods['total'];
					}
					$goods_jdfee=intval($goods_jdfee*100)/100;
					$jdfee +=$goods_jdfee;
				}
			}
			if($this->needorder && $order_price>0){
                    if(substr($gxfee_rate1,-1)=='%'){
                        $goods_gxfee1=($order_price*substr($gxfee_rate1,0,-1))/100;
                    }else{
                        $goods_gxfee1=$gxfee_rate1;
                    }
					$goods_gxfee1=intval($goods_gxfee1*100)/100;
                    $gxfee1 +=$goods_gxfee1;
                    if(substr($gxfee_rate2,-1)=='%'){
                        $goods_gxfee2=($order_price*substr($gxfee_rate2,0,-1))/100;
                    }else{
                        $goods_gxfee2=$gxfee_rate2;
                    }
					$goods_gxfee2=intval($goods_gxfee2*100)/100;
                    $gxfee2 +=$goods_gxfee2;
					if(substr($gxfee_rate3,-1)=='%'){
                        $goods_gxfee3=($order_price*substr($gxfee_rate3,0,-1))/100;
                    }else{
                        $goods_gxfee3=$gxfee_rate3;
                    }
					$goods_gxfee3=intval($goods_gxfee3*100)/100;
                    $gxfee3 +=$goods_gxfee3;
                    if(substr($tui_rate,-1)=='%'){
                        $goods_tuifee=($order_price*substr($tui_rate,0,-1))/100;
                    }else{
                        $goods_tuifee=$tui_rate;
                    }
					$goods_tuifee=intval($goods_tuifee*100)/100;
                    $tuifee +=$goods_tuifee;
                    if(substr($jd_rate,-1)=='%'){
                        $goods_jdfee=($order_price*substr($jd_rate,0,-1))/100;
                    }else{
                        $goods_jdfee=$jd_rate;
                    }
					$goods_jdfee=intval($goods_jdfee*100)/100;
                    $jdfee +=$goods_jdfee;
			}
			//消费贡献值
			if($gxfee1>0 && $commission_level==0){
				$this->commission_order['jifen']+=$gxfee1;
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'level'=>$commission_level,
						'ordersn'=>$this->order['ordersn'],
						'jifen'=>$gxfee1,
						'createtime'=>TIMESTAMP,
						'day'=>date('Y-m-d',TIMESTAMP),
						'year'=>date('Y',TIMESTAMP),
						'status'=>2,
						'type'=>1,
				);
				$this->insert('commission',$new_commission);
                $this->update('member',array('gxfee +='=>$gxfee1),array('mid'=>$member['mid'], 'uniacid' => $_SESSION['uniacid']));
			}
            //1代推荐贡献值
            if($gxfee2>0 && $commission_level==1){
                $this->commission_order['jifen']+=$gxfee2;
                $new_commission=array(
                    'uniacid'=>$_SESSION['uniacid'],
                    'mid'=>$this->agent_id1?:$member['mid'],
                    'level'=>$commission_level,
                    'ordersn'=>$this->order['ordersn'],
                    'jifen'=>$gxfee2,
                    'createtime'=>TIMESTAMP,
					'day'=>date('Y-m-d',TIMESTAMP),
					'year'=>date('Y',TIMESTAMP),
                    'status'=>2,
                    'type'=>2,
                );
                $this->insert('commission',$new_commission);
                $this->update('member',array('gxfee +='=>$gxfee2),array('mid'=>$member['mid'], 'uniacid' => $_SESSION['uniacid']));
            }
			//2+代推荐贡献值（比如1300的商品分5代，3900的商品可以分8代）
            if($gxfee3>0 && (!$candai || $commission_level<$candai) && $commission_level>1){
                $this->commission_order['jifen']+=$gxfee3;
                $new_commission=array(
                    'uniacid'=>$_SESSION['uniacid'],
                    'mid'=>$member['jie_id']?:$member['mid'],
                    'level'=>$commission_level,
                    'ordersn'=>$this->order['ordersn'],
                    'jifen'=>$gxfee3,
                    'createtime'=>TIMESTAMP,
					'day'=>date('Y-m-d',TIMESTAMP),
					'year'=>date('Y',TIMESTAMP),
                    'status'=>2,
                    'type'=>8,
                );
                $this->insert('commission',$new_commission);
                $this->update('member',array('gxfee +='=>$gxfee3),array('mid'=>$member['mid'], 'uniacid' => $_SESSION['uniacid']));
            }
            //领导奖
            if($this->commission_order['commission_money'] && $commission_level && $member['redfee']>0){
                $commission_money = $this->commission_order['commission_money'];
                if ($commission_money > 0) {
                    if (substr($ld_rate, -1) == '%') {
                        $ldfee = ($commission_money * substr($ld_rate, 0, -1)) / 100;
                    } else {
                        $ldfee = $ld_rate;
                    }
					$ldfee=$member['redfee']>$ldfee?$ldfee:$member['redfee'];
                    if($ldfee>0){
						if($member['jie_id']){
							$mid=$member['jie_id'];
						}else{
							$mid=$member['mid'];
						}
                        $this->commission_order['commission_money']+=$ldfee;
                        $new_commission=array(
                            'uniacid'=>$_SESSION['uniacid'],
                            'mid'=>$mid,
                            'level'=>$commission_level,
                            'ordersn'=>$this->order['ordersn'],
                            'money'=>$ldfee,
                            'createtime'=>TIMESTAMP,
							'day'=>date('Y-m-d',TIMESTAMP),
							'year'=>date('Y',TIMESTAMP),
                            'status'=>1,
                            'type'=>6,
                        );
                        $this->insert('commission',$new_commission);
						$this->model('member')->upredfee($member['mid'],$ldfee);
                    }
                }
            }
            //直推奖励
            if($tuifee>0 && $commission_level==1 && $member['redfee']>0){
				$tuifee=$member['redfee']>$tuifee?$tuifee:$member['redfee'];
				if($tuifee>0){
					$this->commission_order['commission_money']+=$tuifee;
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$this->agent_id1?:$member['mid'],
						'level'=>$commission_level,
						'ordersn'=>$this->order['ordersn'],
						'money'=>$tuifee,
						'createtime'=>TIMESTAMP,
						'day'=>date('Y-m-d',TIMESTAMP),
						'year'=>date('Y',TIMESTAMP),
						'status'=>1,
						'type'=>3,
					);
					$this->insert('commission',$new_commission);
					$this->model('member')->upredfee($member['mid'],$tuifee);
				}
            }
			//见点奖励
			if($jdfee>0 && $commission_level && $member['redfee']>0){
                /*$teamids=$this->getteamids($member['mid']);
                $num=count($teamids);
                if($num){
                    $onefee=$jdfee/$num;
                }*/
				 $jdfee=$member['redfee']>$jdfee?$jdfee:$member['redfee'];
                if($jdfee>0){
                    $this->commission_order['commission_money']+=$jdfee;
					if($member['jie_id']){
						$mid=$member['jie_id'];
					}else{
						$mid=$member['mid'];
					}
                   $new_commission=array(
                            'uniacid'=>$_SESSION['uniacid'],
                            'mid'=>$mid,
                            'level'=>$commission_level,
                            'ordersn'=>$this->order['ordersn'],
                            'money'=>$jdfee,
                            'jifen'=>0,
                            'createtime'=>TIMESTAMP,
							'day'=>date('Y-m-d',TIMESTAMP),
							'year'=>date('Y',TIMESTAMP),
                            'status'=>1,
                            'type'=>4,
                     );
                     $this->insert('commission',$new_commission);
					 $this->model('member')->upredfee($member['mid'],$jdfee);
                }
			}
			$this->model('member')->update_agent_level($member['mid'],$this->order['id']);
		}
		return $member['agent_id'];
	}
	public function get_orderfee($mid=0){
		$teamids=$this->model('member')->getteamids($mid);
		if($teamids){
			$alljifen=$this->getsum('commission',array('ordersn'=>$this->order['ordersn'],'mid'=>$teamids),'jifen');
		}
		return $alljifen;
	}
	/*
		订单完成时，把佣金状态由初始的订单待完成升级为待结算
	*/
	public function order_complete($order){
		$this->update('commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('ordersn'=>$order['ordersn'],'status'=>0));
	}
	/*
		创建结算单
	*/
	public function create_balance($mid,$post=array()){
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		if($post['pay_type']=='downline'){
			if(!$post['wechat']){
				exi('请先填写微信号！','error');
			}
			if(!$post['mobile']){
				exi('请先填写联系电话！','error');
			}
		}
		if($post['pay_type']=='alipay'){
			if(!$post['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$post['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$post['alipay1']){
				exi('请确认帐号','error');
			}
			if($post['alipay1']!=$post['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
			$typetitle='支付宝';
		}
		if($post['pay_type']=='union'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$post['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$post['bankcard1']){
				exi('请确认卡号','error');
			}
			if($post['bankcard']!=$post['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
			$typetitle='银行卡';
		}
		//获取可结算佣金
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1,'isxuni'=>0);
		if($this->setting['balance_day']){
			//结算天数
			$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
			$params['complete_time <']=$time;
		}
		$money=pdo_getsum('jy_baojfee_commission',$params,'money');
		if($money<=0){
			exi('没有可结算的佣金！','error');
		}
		$commission_num=$this->count('commission',$params,'id');
		//结算最低佣金检测
		if($money<$this->setting['balance_money']){
			exi('可结算佣金太少，无法结算！','error');
		}
		if($this->setting['balance_difee']){
			$lastcredit=pdo_getcolumn('core_members',array('id'=>$mid),'credit1');
			if($lastcredit<$money){
				exi('提现需扣除等量积分，您当前积分不足，仅剩'.$lastcredit.'！','error');
			}else{
				cfc('core')->member_credit($mid,'credit1',$money,false,'jy_baojfee','提现扣除等量积分');
			}
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'commission_num'=>$commission_num,
			'money'=>$money,
			'pay_type'=>$post['pay_type'],
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'wechat'=>$post['wechat'],
			'mobile'=>$post['mobile'],
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		//结算手续费
		if($this->setting['balance_per']){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		$update=array(
			'status'=>2,
			'balancesn'=>$new_balance['balancesn'],
		);
		$this->update('commission',$update,$params);
		if($post['pay_type']=='credit'){
			$typetitle='余额';
			if($this->setting['credit_pass']){
				$this->check_balance($balance_id,1,'免审打款到余额');
				$this->pay_balance($balance_id,'credit');
			}
		}
		if($post['pay_type']=='wechat'){
			$typetitle='微信';
		}
		$this->model('notice')->notice_fenxiao_tixian($mid,$money,$typetitle);
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('balance',$update,array('id'=>$balance_id));
		return true;
		/*$update=array(
			'status'=>1,
			'balancesn'=>'',
		);
		$balance=$this->get('balance',array('id'=>$balance_id),array('mid','balancesn'));
		if($balance['balancesn']){
			$this->update('commission',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$balance['mid'],'balancesn'=>$balance['balancesn']));
		}*/
		return true;
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type='',$wxgid='',$pay_trade_no=''){
		$balance=$this->get('balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			//exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn'],$wxgid);
		$update=array(
			'status'=>3,
			'pay_trade_no'=>$trade_no=='pay_trade_no'?$pay_trade_no:$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		if($pay_type){
			$update['pay_type']=$pay_type;
		}
		$this->update('balance',$update,array('id'=>$balance['id']));
		$this->model('notice')->notice_fenxiao_dakuan($balance['mid'],$balance['final_money']);
		return true;
	}
	/*
		线下打款
	*/
	protected function pay_balance_by_downline(){
		return 'pay_trade_no';
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,'jy_baojfee','分红佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn,$wxgid=''){
		if($wxgid){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
		}
		$params=array(
			'appid'=>$wxapp['appid']?:'',
			'mch_id'=>$wxapp['mchid']?:'',
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'wxgid'=>$wxgid,
			'remark'=>'代理商佣金发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		if(!$res['payment_no']){
			exi('打款到微信零钱失败！','error');
		}
		return $res['payment_no'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}

	public function commission_remark($remark){
		if(!$remark){
			return '';
		}
		$remark=json_decode($remark,true);
		$goods_str='';
		if($remark['goods_commission']){
			foreach($remark['goods_commission'] as $goods){
				$goods['title']=pdo_getcolumn('jy_weishop_goods',array('id'=>$goods['goods_id']),'title');
				$goods_str.="商品ID【{$goods['goods_id']}】，商品名称【{$goods['title']}】，商品数量【{$goods['total']}】，商品总价【{$goods['total_price']}】，佣金规则【{$goods['commission_per']}】，分红佣金【{$goods['commission_money']}】\n";
			}
		}else{
			$goods_str='未启用';
		}
		$remark['level_title']=pdo_getcolumn('jy_baojfee_level',array('uniacid'=>$_SESSION['uniacid'],'level'=>$remark['commission_level']),'title');
		$remark['level_title']=$remark['level_title']?:'默认等级';
		$remark_str=<<<TPL
加盟商等级ID【{$remark['level_id']}】，等级名称【{$remark['level_title']}】，佣金等级【{$remark['commission_level']}】，订单总价【{$remark['order_price']}】
商品独立佣金：
{$goods_str}
订单佣金：
除去设置独立佣金的商品外订单金额【{$remark['order_commission']['order_price']}】，佣金规则【{$remark['order_commission']['commission_per']}%】，分红佣金【{$remark['order_commission']['commission_money']}】
佣金总计：【{$remark['commission_money']}】
TPL;
		return $remark_str;
	}
	/*
		获取单独商品佣金
	*/
	protected function get_goodsfee($goodsid,$orderid,$ordergoodsid=''){
		$ordersn=pdo_getcolumn('jy_weishop_order',array('id'=>$orderid,'uniacid'=>$_SESSION['uniacid']),'ordersn');
		$commissionremark=$this->pdo_getcolumn('commission',array('ordersn'=>$ordersn,'uniacid'=>$_SESSION['uniacid']),'remark');
		//print_r($ordersn.'~~'.$commissionremark);exit;
		$commissionremark=json_decode($commissionremark,true);
		
		$goodsfee=0;
		if($commissionremark['goods_commission']){
			foreach($commissionremark['goods_commission'] as $gv){
				if($ordergoodsid){
					if($ordergoodsid==$gv['ordergoodsid']){
						$goodsfee +=$gv['commission_money'];
					}
				}else{
					if($goodsid==$gv['goods_id']){
						$goodsfee +=$gv['commission_money'];
					}
				}
			}
		}
		if($commissionremark['order_commission'] && $goodsfee==0){
			$allorderfee=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$orderid,'goodsid'=>$goodsid),'total_price');
			//exi($allorderfee.'~'.$commissionremark['order_commission']['order_price'].'~'.$commissionremark['order_commission']['commission_money'],'error');
			$goodsfee=($allorderfee/$commissionremark['order_commission']['order_price'])*$commissionremark['order_commission']['commission_money'];
		}
		return $goodsfee;
	}
}